import util from '@/common/util.js'
import base64Src from '@/common/base64Src.js'

//分享统计
const shareStatistics = function(userId,id,type){
	util.getAjax(util.apiUrl + '/fx',(res)=>{
		console.log(res);
	},{user_id: userId,type: type,son_id: id})
}

//获取二维码图片
const getWxCode = function(page,scene){
	return new Promise((resolve,reject)=>{
		util.getAjax(util.apiUrl + '/getWxcode',(res)=>{
			console.log(res);
			base64Src.base64Src(res.data, code => {
				console.log(code)
				resolve(code); 
			})
		},{page: page,scene: scene})
	})
}

const imgBtn = function(list,index,type){
	// console.log(e)
	var arr = []
	var idx = Number
	switch (type){
		case 'index':
			if(list[index].type == 0){
				for(var i in list){
					arr.push(list[i].banner)
				}
				uni.previewImage({
				  urls: arr,
				  current: arr[index]
				})
			}else{
				if(list[index].type == 1){
					//商品
					uni.navigateTo({
						url: '/subPackages/goods/goodsDetails/goodsDetails?id=' + list[index].fid
					})
				}else if(list[index].type == 2){
					//店铺
					uni.navigateTo({
						url: '/subPackages/goods/shopAre/shopAre?shopId=' + list[index].fid
					})
				}
			}
			break;
		case 'goodsDetails':
			if(list[0].type == 'video'){
				idx = Number(index) - 1
				for(var i in list){
					if(list[i].type == 'pic'){
						arr.push(list[i].url)
					}
				}
			}else if(list[0].type == 'pic'){
				idx = Number(index)
				for(var i in list){
					arr.push(list[i].url)
				}
			}
			uni.previewImage({
			  urls: arr,
			  current: arr[idx]
			})
			break;
		case 'evaluation':
			for(var i in list){
				arr.push(list[i])
			}
			uni.previewImage({
			  urls: arr,
			  current: arr[index]
			})
			break;
	}
}
 
const setIntegral = function(userId,ruleType,integralType){
	
	return new Promise((resolve,reject)=>{
		let isIntegral = true
		console.log(ruleType);
		util.getAjax(util.apiUrl + '/get_user_num',(res)=>{
			console.log(res);
			if(res.data != 0){
				if(ruleType == 1){
					isIntegral = false
					resolve()
				}else if(ruleType == 2){
					if(res.data == 5){
						isIntegral = false
						resolve()
					}
				}
			}
			if(isIntegral){
				util.getAjax(util.apiUrl + '/get_integral_rule',(res)=>{
					console.log(res);
					let data = {
						user_id: userId,
						log: res.data.log,
						integral: res.data.integral,
						type: integralType
					}
					data = JSON.stringify(data)
					util.getAjax(util.apiUrl + '/user_get_integral',(integral)=>{
						if(integral.data.code == 200){
							//成功
							resolve(res.data)
						}
					},{pro: data,type: ruleType})
				},{id: ruleType})
			}
		},{type: ruleType,user_id: userId})
	})
}


const integralAni = function(){
	return new Promise((resolve,reject)=>{
		setTimeout(()=>{
			resolve(false)
		},3000)
	})
}

export default{
	shareStatistics,
	getWxCode,
	imgBtn,
	setIntegral,
	integralAni
}